package com.ruoyi.web.controller.system;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysAppPush;
import com.ruoyi.system.service.ISysAppPushService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * APP推送管理Controller
 *
 * @author shuyan
 * @date 2021-12-07
 */
@RestController
@RequestMapping("/system/appPush")
public class SysAppPushController extends BaseController {

    @Autowired
    private ISysAppPushService sysAppPushService;
    @Autowired
    private TokenService tokenService;


    /**
     * 查询APP推送管理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SysAppPush sysAppPush) {
        startPage();
        List<SysAppPush> list = sysAppPushService.queryList(sysAppPush);
        return getDataTable(list);
    }

    /**
     * 获取APP推送管理详细信息
     */
    @GetMapping(value = "/{clientId}")
    public AjaxResult getInfo(@PathVariable("clientId") String clientId) {
        return AjaxResult.success(sysAppPushService.getById(clientId));
    }

    /**
     * 新增APP推送管理
     */
    @Log(title = "APP推送管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysAppPush sysAppPush) {
        sysAppPush.setCreateTime(new Date());
        return toAjax(sysAppPushService.save(sysAppPush));
    }

    /**
     * 修改APP推送管理
     */
    @Log(title = "APP推送管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysAppPush sysAppPush) {
        sysAppPush.setUpdateTime(new Date());
        return toAjax(sysAppPushService.updateById(sysAppPush));
    }

    /**
     * 删除APP推送管理
     */
    @Log(title = "APP推送管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{clientIds}")
    public AjaxResult remove(@PathVariable String[] clientIds) {
        return toAjax(sysAppPushService.removeByIds(Arrays.asList(clientIds)));
    }

    //业务内容接口============================================================================

    /**
     * 绑定设备
     */
    @PostMapping("/bindClient")
    public AjaxResult bindClient(@RequestBody SysAppPush client) {
        //绑定个推
        sysAppPushService.bindClient(client);
        //设置loginUser的推送客户端ID
        LoginUser loginUser = SecurityUtils.getLoginUser();
        loginUser.setClientId(client.getClientId());
        tokenService.setLoginUser(loginUser);
        return AjaxResult.success("操作完成");
    }
}
